using Godot;
using System;
using System.Threading.Tasks;
public partial class Digia : Control
{
	[Export]
	private RichTextLabel dialogueLabel;
	[Export]
	private Sprite2D _digiaSprite;
	[Export]
	private Sprite2D _playFaceSprite;
	[Export]
	private Sprite2D _bossFaceSprite;
	[Export]
	private Sprite2D _oldmanFaceSprite;
	[Export]
	private Sprite2D _oldwomanFaceSprite;
	[Export]
	private Sprite2D _wnFaceSprite;
	[Export]
	private Sprite2D _fakeFaceSprite;
	
	private float charactersPerSecond = 20f;
	[Signal]
	public delegate void MyCallableSignalEventHandler();
	[Signal]
	public delegate void EndDiaSignalEventHandler();
	private string fullText;
	private bool isTyping = false;
	private async Task TypeTextCoroutine(string text)
	{
		fullText = text;
		dialogueLabel.Text = "";
		isTyping = true;

		float delay = 1f / charactersPerSecond;

		for (int i = 0; i < text.Length; i++)
		{
			dialogueLabel.Text += text[i];

			// 等待指定的时间
			await ToSignal(GetTree().CreateTimer(delay), "timeout");

			// 如果被跳过，提前结束
			if (!isTyping)
			{
				dialogueLabel.Text = fullText;
				break;
			}
		}

		isTyping = false;
		await WaitForEnterKey();
	}

	// 开始对话
	public async void StartDialogue(string text, int tag)
	{
		if (tag == 0)
		{
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = true;
			_fakeFaceSprite.Visible = false;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}

			await TypeTextCoroutine(text);
			EmitSignal(SignalName.MyCallableSignal);
		}
		else if (tag == 1)
		{
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = false;
			_fakeFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine(text);
			EmitSignal(SignalName.MyCallableSignal);
		}
		else if (tag == 2)
		{
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = true;
			_fakeFaceSprite.Visible = false;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("这张卡片为什么是空白的");
			
			
			
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = false;
			_fakeFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("这就是你丢失的记忆，因为你现在还没有恢复记忆，所以卡片的内容还是一片空白，努力收集白色卡片吧");
			EmitSignal(SignalName.EndDiaSignal);
		}else if (tag == 3)
		{
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = true;
			_fakeFaceSprite.Visible = false;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("什么声音");
			
			
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = false;
			_fakeFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("好像是一群称为铠甲勇士的中二少年");
			
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = true;
			_fakeFaceSprite.Visible = false;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("呦西，他们为看起来就不像好人");
			
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = false;
			_fakeFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("不要啰嗦了，赶紧打败他们吧");
			
			EmitSignal(SignalName.EndDiaSignal);
		}else if (tag == 4)
		{
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = true;
			_fakeFaceSprite.Visible = false;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("啊啊啊，他们怎么好像合体了");
			
			EmitSignal(SignalName.EndDiaSignal);
		}else if (tag == 5)
		{
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = false;
			_fakeFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("太好了，完成了");
			
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = true;
			_fakeFaceSprite.Visible = false;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("我们已经获得所有的白色卡片了吗，是不是我的记忆就能恢复了");
			
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = false;
			_bossFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("哈哈哈，太好了，这样你的记忆就全部消失了");
			
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = true;
			_bossFaceSprite.Visible = false;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("！！！！，你究竟是谁");
			
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = false;
			_bossFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("我是阿尔茨.海默，专门吞食人类的记忆，我之前一直伪装成你最爱的人的样子，就在刚刚，所有想把你拉回来的朋友都被你自己消灭了");
			
			
			
			EmitSignal(SignalName.EndDiaSignal);
		}else if (tag == 6)
		{
			_digiaSprite.Visible = true;
			_oldmanFaceSprite.Visible = false;
			_oldwomanFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("老头子，你还记得这里吗，我们刚谈恋爱的时候经常来这里闲逛");
			
			
			_digiaSprite.Visible = true;
			_oldmanFaceSprite.Visible = true;
			_oldwomanFaceSprite.Visible = false;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("呜呜。。。。。。，啊啊。。。。。。。");
			
			_digiaSprite.Visible = true;
			_oldmanFaceSprite.Visible = false;
			_oldwomanFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("哎，你肯定想不起来了，你已经.....");
			
			_digiaSprite.Visible = true;
			_oldmanFaceSprite.Visible = true;
			_oldwomanFaceSprite.Visible = false;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("呜呜。。。。。。，啊啊。。。。。。。");
			
			_digiaSprite.Visible = true;
			_oldmanFaceSprite.Visible = false;
			_oldwomanFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("我年轻的时候特别喜欢说，我们俩真是天生一对，一样喜欢游戏，一样喜欢动漫，你那时候还经常和我讲铠甲勇士、光能使者。。。。。");
			await TypeTextCoroutine("你还记得我们养的第一只小狗维尼吗。。。");
			await TypeTextCoroutine("。。。。。");
			EmitSignal(SignalName.EndDiaSignal);
		}else if (tag == 7)
		{
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("刚才脑子里闪过的是什么");
			await TypeTextCoroutine("为什么这么想去见那个人");
			await TypeTextCoroutine("我是。。。。");
			await TypeTextCoroutine("我是。。。。");
			await TypeTextCoroutine("我到底是。。。。");
			
			EmitSignal(SignalName.EndDiaSignal);
		}else if (tag == 8)
		{
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("它们是我之前打败过的魔王部下,这是他们的原来的样子吗");
			
			_fakeFaceSprite.Visible = true;
			_playFaceSprite.Visible = false;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("我是你最爱的宝宝啊，你不记得我了吗");

			
			_wnFaceSprite.Visible = true;
			_fakeFaceSprite.Visible = false;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("汪汪");
			
			
			EmitSignal(SignalName.EndDiaSignal);
		}else if (tag == 9)
		{
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = true;
			await TypeTextCoroutine("我是。。。。。。");
			await TypeTextCoroutine("我是赵迎志，是要成为超高校级程序员的男人");
			
			_playFaceSprite.Visible = false;
			_bossFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("怎么可能，你的记忆不是已经被抹去了吗");
			
			
			_playFaceSprite.Visible = true;
			_bossFaceSprite.Visible = false;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("你说的没错");
			await TypeTextCoroutine("但是");
			await TypeTextCoroutine("我还要和那个人一起长命百岁，怎么能在这里就停下呢");
			await TypeTextCoroutine("你说是不是");
			await TypeTextCoroutine("超高校级可爱");
			
			EmitSignal(SignalName.EndDiaSignal);
		}
		else if (tag == 10)
		{
			_wnFaceSprite.Visible = true;
			_digiaSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("汪汪,汪汪汪");
			
			_wnFaceSprite.Visible = false;
			_playFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("维尼，别舔我了，定、定。。。。。");
			
			
			EmitSignal(SignalName.EndDiaSignal);
		}else if (tag == 11)
		{
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("宝宝，让你久等了，我回来了");
			
			_fakeFaceSprite.Visible = true;
			_playFaceSprite.Visible = false;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("你也太慢了，回头我要好好惩罚你");
			
			
			EmitSignal(SignalName.EndDiaSignal);
		}else if (tag == 12)
		{
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("该我们上场了");
	
			
			EmitSignal(SignalName.EndDiaSignal);
		}else if (tag == 13)
		{
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("一起上吧");
	
			
			EmitSignal(SignalName.EndDiaSignal);
		}else if (tag == 14)
		{
			_digiaSprite.Visible = true;
			_playFaceSprite.Visible = true;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("超高校级一家攻击");
			
			_fakeFaceSprite.Visible = true;
			_playFaceSprite.Visible = false;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			_fakeFaceSprite.Visible = false;
			_wnFaceSprite.Visible = true;
			await TypeTextCoroutine("汪汪汪");
		
			EmitSignal(SignalName.EndDiaSignal);
		}else if (tag == 15)
		{
			_digiaSprite.Visible = true;
			_oldwomanFaceSprite.Visible = true;
			
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("维尼特别不听话，还经常窜稀，然后你边骂边处理他的屎");
			
			_oldmanFaceSprite.Visible = true;
			_oldwomanFaceSprite.Visible = false;
			if (isTyping)
			{
				SkipTyping();
				await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
			}
			await TypeTextCoroutine("宝.....,宝......，宝宝......");
			
		
			EmitSignal(SignalName.EndDiaSignal);
		}
		else
		{


		}
		_wnFaceSprite.Visible = false;
		_oldmanFaceSprite.Visible = false;
		_oldwomanFaceSprite.Visible = false;
		_digiaSprite.Visible = false;
		_playFaceSprite.Visible = false;
		_fakeFaceSprite.Visible = false;
		_bossFaceSprite.Visible = false;
	}
	private async Task WaitForEnterKey()
	{
		while (!Input.IsActionPressed("NextDia"))
		{
			await ToSignal(GetTree(), "process_frame");
		}
	}
	// 跳过打字
	public void SkipTyping()
	{
		isTyping = false;
	}
}
